/*
 * Copyright (c) 2014, Asiainfo-Linkage. All rights reserved.
 * Asiainfo-Linkage PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package edu.w106.platform.http;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

/**
 * <p><b>标题：</b>HttpPostUtils.</p>
 *
 * <p><b>描述：</b></p>
 *
 * <p><b>版权：</b>Copyright (c) 2014 亚信联创</p>
 *
 * <p><b>工程：</b>w106-mc</p>
 *
 * @author w106
 *
 * @version 1.0.0
 *
 * @since 2014-5-4 下午4:57:45
 */
public class HttpPostUtils extends HttpClientUtils {

    /**
     * 发送请求.
     * @param uri 资源的相对地址
     * @param xmldata 请求的xml数据.
     * @return 处理结果
     */
    public Response sendRequest(Map<String, String> postdata, String uri) {
        String response = null;

        // 因为后端服务目前没有完全采用rest风格，所以方法的严格区分毫无意义，因此默认都采用post发送
        try {
            HttpPost httpPost = new HttpPost(uri);

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            for (String key : postdata.keySet()) {
                nvps.add(new BasicNameValuePair(key, postdata.get(key)));
            }
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, DEFAULT_CHARSET));
            return sendRequest(httpPost);
        } catch (Exception e) {
            Response resp = new Response(uri);
            resp.setResponse(e.getMessage());
            return resp;
        }
    }
}
